/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */
using UnityEditor;
using UnityEngine;

namespace TFrameWork.Module.ActionEditor
{
    public class TriggerEventItemStyle : CommonEventItemStyle
    {
        public override Color eventColor => new Color(0.69f, 0.98f, 0.36f, 0.4f);

        //默认长度为10帧
        public override int RealLenth => int.MaxValue;

        public override TrackEventBase eventData => _triggerEvent;
        private TriggerEvent _triggerEvent;


        public void Init(TriggerTrackStyle.CommonChildTrackBase parent, TriggerEvent data)
        {
            _triggerEvent = data;
            InitComponent(parent);
        }

        public override void ShowContent()
        {
            base.ShowContent();
            Name.text = _triggerEvent.trackName;
        }


        #region Scene编辑

        public override void DrawGizmos()
        {
            if (ActionEditor.Instance.CurrentSelectFrameIndex >= _triggerEvent.startIndex &&
                ActionEditor.Instance.CurrentSelectFrameIndex <= _triggerEvent.startIndex + _triggerEvent.durationIndex)
            {
                Transform relativeTran = ActionEditor.Instance.PreviewCharacterObj.transform;
                if (_triggerEvent.recordTransform != null)
                {
                    relativeTran = _triggerEvent.recordTransform;
                }
                else
                {
                    var findTran = ActionEditorUtility.FindChildTransformByNameIndex(
                        ActionEditor.Instance.PreviewCharacterObj.transform, _triggerEvent.rocordTransformName);
                    if (findTran != null)
                    {
                        relativeTran = findTran;
                    }
                }

                var gColor = Gizmos.color;
                Gizmos.color = new Color(0, 1, 0, 0.2f);

                if (eventData is BoxTriggerEvent box)
                {
                    var boxPos = box.position;
                    var boxRotation = Quaternion.Euler(box.rotate);
                    var boxScale = box.scale;
                    var boxLocalMat = Matrix4x4.TRS(boxPos, boxRotation, boxScale);

                    int CurrentSelectFrameIndex = ActionEditor.Instance.CurrentSelectFrameIndex;
                    if (_triggerEvent.recordEventDatas.TryGetValue(CurrentSelectFrameIndex, out var record) &&
                        ActionEditor.Instance.currentSelected != this)
                    {


                        //当有录制内容，且当前没有选中自己，播放录制内容
                        var mat = ActionEditor.Instance.PreviewCharacterObj.transform.localToWorldMatrix *
                                  record.localToRelativeRootWrold * boxLocalMat;
                        Gizmos.matrix = mat;
                        Gizmos.DrawCube(Vector3.zero, box.size);
                        var gColor2 = Gizmos.color;
                        Gizmos.color = new Color(1, 0, 0, 0.8f);
                        Gizmos.DrawWireCube(Vector3.zero, box.size);
                        Gizmos.color = gColor2;
                    }
                    else
                    {
                        var mat = relativeTran.transform.localToWorldMatrix * boxLocalMat;
                        Gizmos.matrix = mat;
                        Gizmos.DrawCube(Vector3.zero, box.size);
                        var gColor2 = Gizmos.color;
                        Gizmos.color = new Color(0, 1, 0, 0.8f);
                        Gizmos.DrawWireCube(Vector3.zero, box.size);
                        Gizmos.color = gColor2;
                    }
                }
                else if (eventData is SphereTriggerEvent sphere)
                {
                    var spherePos = sphere.position;
                    var sphereRotation = Quaternion.Euler(sphere.rotate);
                    var sphereScale = sphere.scale;
                    var sphereLocalMat = Matrix4x4.TRS(spherePos, sphereRotation, sphereScale);
                    int CurrentSelectFrameIndex = ActionEditor.Instance.CurrentSelectFrameIndex;
                    if (_triggerEvent.recordEventDatas.TryGetValue(CurrentSelectFrameIndex, out var record) &&
                        ActionEditor.Instance.currentSelected != this)
                    {
                        var mat = ActionEditor.Instance.PreviewCharacterObj.transform.localToWorldMatrix *
                                  record.localToRelativeRootWrold * sphereLocalMat;
                        Gizmos.matrix = mat;
                        var gColor2 = Gizmos.color;
                        Gizmos.DrawSphere(Vector3.zero, sphere.radius);
                        Gizmos.color = new Color(1, 0, 0, 0.8f);
                        Gizmos.DrawWireSphere(Vector3.zero, sphere.radius);
                        Gizmos.color = gColor2;
                    }
                    else
                    {
                        var mat = relativeTran.transform.localToWorldMatrix * sphereLocalMat;
                        Gizmos.matrix = mat;
                        Gizmos.DrawSphere(Vector3.zero, sphere.radius);
                        var gColor2 = Gizmos.color;
                        Gizmos.color = new Color(0, 1, 0, 0.8f);
                        Gizmos.DrawWireSphere(Vector3.zero, sphere.radius);
                        Gizmos.color = gColor2;
                    }


                }
                else if (eventData is SectorTriggerEvent sector)
                {

                }

                Gizmos.color = gColor;
                Gizmos.matrix = Matrix4x4.identity;
            }
        }

        bool isUndo = false;

        public override void OnSceneGUI()
        {
            if (ActionEditor.Instance.currentSelected == this &&
                (ActionEditor.Instance.CurrentSelectFrameIndex >= _triggerEvent.startIndex &&
                 ActionEditor.Instance.CurrentSelectFrameIndex <=
                 _triggerEvent.startIndex + _triggerEvent.durationIndex))
            {
                Transform relativeTran = ActionEditor.Instance.PreviewCharacterObj.transform;
                if (_triggerEvent.recordTransform != null)
                {
                    relativeTran = _triggerEvent.recordTransform;
                }
                else
                {
                    var findTran = ActionEditorUtility.FindChildTransformByNameIndex(
                        ActionEditor.Instance.PreviewCharacterObj.transform, _triggerEvent.rocordTransformName);
                    if (findTran != null)
                    {
                        relativeTran = findTran;
                    }
                }

                if (_triggerEvent is BoxTriggerEvent box)
                {
                    var pos = relativeTran.transform.TransformPoint(_triggerEvent.position);
                    var rotate = Quaternion.Euler(box.rotate) * relativeTran.transform.rotation;

                    EditorGUI.BeginChangeCheck();
                    Handles.TransformHandle(ref pos, ref rotate, ref box.scale);

                    if (EditorGUI.EndChangeCheck())
                    {
                        box.position = relativeTran.transform.InverseTransformPoint(pos); //世界坐标转换成相对月preview对象的相对坐标
                        box.rotate = (rotate * Quaternion.Inverse(relativeTran.transform.rotation)).eulerAngles;
                        ActionEditorInspector.Instance.Show();
                    }
                }
                else if (_triggerEvent is SphereTriggerEvent sphere)
                {
                    var oldPos = relativeTran.transform.TransformPoint(_triggerEvent.position);
                    var newPos = Handles.PositionHandle(oldPos, Quaternion.identity);

                    float newRadius = Handles.ScaleSlider(sphere.radius, newPos, Vector3.back, Quaternion.identity,
                        sphere.radius + 0.3f,
                        0.1f);
                    if (oldPos != newPos || sphere.radius != newRadius)
                    {

                        sphere.position = relativeTran.transform.InverseTransformPoint(newPos);
                        sphere.radius = newRadius;
                        ActionEditorInspector.Instance.Show();
                    }
                }
            }
        }

        #endregion
    }
}
